package com.qianli.cashmama.trade.repo;

import com.qianli.cashmama.trade.dao.RiskMhReportContentDao;
import com.qianli.cashmama.trade.entity.RiskMhReportContentEntity;
import com.qianli.cashmama.trade.openApi.vendor.inner.vos.MhReportContentVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2020-07-07
 * Time: 14:40
 */
@Repository
public class SmsRepoImpl implements SmsRepo {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmsRepoImpl.class);

    @Autowired
    private RiskMhReportContentDao riskMhReportContentDao;


    @Override
    public void storeMhReportRequest(String riskNo, String tradeNo, String smsUrl) {
        RiskMhReportContentEntity riskMhReportContentEntity = new RiskMhReportContentEntity()
                .setRiskNo(riskNo)
                .setTradeNo(tradeNo)
                .setSmsUrl(smsUrl);

        riskMhReportContentDao.insert(riskMhReportContentEntity);
    }

    @Override
    public void storeMhReportRes(String riskNo, String tradeNo, String content, String netContent) {

        LOGGER.info("== 存储mh report ==,riskNo:{},tradeNo:{},content:{},netContent:{}", riskNo, tradeNo, content, netContent);

        RiskMhReportContentEntity riskMhReportContentEntity1 = riskMhReportContentDao.selectOneByRiskNo(riskNo);
        if (riskMhReportContentEntity1 == null) {
            LOGGER.info("== 请求不存在 ==");
            RiskMhReportContentEntity riskMhReportContentEntity = new RiskMhReportContentEntity()
                    .setRiskNo(riskNo)
                    .setTradeNo(tradeNo)
                    .setStatus(1)
                    .setContent(content)
                    .setContentNew(netContent);

            riskMhReportContentDao.insert(riskMhReportContentEntity);
            return;
        }

        LOGGER.info("== 请求存在  更新报告状态和内容==");
        riskMhReportContentDao.updateReportByRiskNo(riskNo, content, netContent);
    }

    @Override
    public MhReportContentVo loadByRiskNo(String riskNo) {
        RiskMhReportContentEntity riskMhReportContentEntity = riskMhReportContentDao.selectOneByRiskNo(riskNo);
        if (riskMhReportContentEntity == null) {
            return null;
        }

        return new MhReportContentVo()
                .setContent(riskMhReportContentEntity.getContent())
                .setRiskNo(riskMhReportContentEntity.getRiskNo())
                .setNewContent(riskMhReportContentEntity.getContentNew())
                .setTradeNo(riskMhReportContentEntity.getTradeNo())
                .setStatus(riskMhReportContentEntity.getStatus());
    }


    @Override
    public MhReportContentVo loadByTradeNo(String tradeNo) {
        RiskMhReportContentEntity riskMhReportContentEntity = riskMhReportContentDao.selectLatestOneByTradeNo(tradeNo);
        if (riskMhReportContentEntity == null) {
            return null;
        }

        return new MhReportContentVo()
                .setContent(riskMhReportContentEntity.getContent())
                .setRiskNo(riskMhReportContentEntity.getRiskNo())
                .setNewContent(riskMhReportContentEntity.getContentNew())
                .setTradeNo(riskMhReportContentEntity.getTradeNo())
                .setStatus(riskMhReportContentEntity.getStatus());
    }

}
